package com.umfb.mysql;

import com.umfb.config.zk.ZooKeeperPropertyPlaceholderConfigurer;
import com.umfb.utils.UmfbApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@EnableAutoConfiguration
public class MysqlApplication extends WebMvcConfigurerAdapter{
	private static  final Logger logger = LoggerFactory.getLogger(MysqlApplication.class);

	public static void main(String[] args) {
		SpringApplication springApplication = new SpringApplication (MysqlApplication.class);
		final ApplicationContext ac = springApplication.run(MysqlApplication.class,args);
		UmfbApplicationContext.setApplicationContext(ac);

		logger.info("-------------------Dubbo mysql service started----------------------");
	}

	@Bean
	public UmfbApplicationContext initAppEnd(ApplicationContext ac) {
		UmfbApplicationContext.setApplicationContext(ac);

		return new UmfbApplicationContext();

	}

	private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
	@Bean
	public PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {

		PropertySourcesPlaceholderConfigurer configurer = new ZooKeeperPropertyPlaceholderConfigurer();
		configurer.setIgnoreResourceNotFound(true);
		configurer.setLocations(resourcePatternResolver.getResource("classpath:application.properties"));

		return configurer;
	}

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**").allowedOrigins("*");
	}
}
